package cn.wit.summit.chwan.utils;

import android.app.Activity;
import android.content.SharedPreferences;
import android.util.Base64;
import android.util.Log;
import android.view.View;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import cn.wit.summit.chwan.models.response.RedEnvelopeRespone;
import cn.wit.summit.chwan.models.response.ResponseBase;
import cn.wit.summit.chwan.models.response.SendSystemResponce;
import cn.wit.summit.chwan.net.BuildRequestUtils;
import cn.wit.summit.chwan.net.NetCallBack;
import cn.wit.summit.chwan.net.okhttplib.OkHttpUtil;
import cn.wit.summit.chwan.retrofit.ApiUtil;
import cn.wit.summit.chwan.retrofit.MyCallback;
import cn.wit.summit.chwan.retrofit.RetrofitManager;
import cn.wit.summit.chwan.ui.activity.AccountLoginActivity;
import cn.wit.summit.chwan.ui.activity.MainActivity;
import cn.wit.summit.chwan.ui.base.MyApplication;
import cn.wit.summit.chwan.widgets.RedDataDialog;
import cn.wit.summit.chwan.widgets.RedEnvelopeDialog;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;

/**
 * Created by yangg on 2018/4/28.
 */

public class RedEnvelopeUtils {
    private static RedEnvelopeDialog redDialog;

    /**
     * 获取红包
     *
     * @param activity
     */
    public static void ShowRedEnvelope(final Activity activity) {
        if (MyApplication.accountBean != null) {
            if (MyApplication.accountBean.getNewAccount() == 1) { //1 是新用户
                Log.e("TAG", MyApplication.accountBean.getNewAccount() + "<--");
                if (NetWorkUtils.isNetworkConnected(activity)) {
                    if (redDialog==null){
                        redDialog = new RedEnvelopeDialog(activity);
                        redDialog.show();
                    }

                    redDialog.onClickClose(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            SharedPreferences sp = MyApplication.getMyApplication().getRedps();
                            SharedPreferences.Editor editor = sp.edit();
                            editor.putBoolean("isclose", false);
                            editor.commit();
                            redDialog.dismiss();
                            redDialog=null;
                        }
                    });

                    redDialog.onClickShow(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            redDialog.dismiss();
                            //然后请求数据
                            getValue(activity);
                            //发送用户信息
                            sendUser(activity);

                        }
                    });

                } else {
                    ToastUtils.getInstance(activity).showToastSystem("网络连接失败，再试试吧~");
                }
            }
        }
    }



    private static void  sendUser(Activity activity){
        //发送用户信息信息
        RegisterBean ri = new RegisterBean();
        ri.setA(IphoneUtils.getImageSize(activity));
        ri.setB(IphoneUtils.getDeviceBrand());
        ri.setC(IphoneUtils.getMac(activity));
        ri.setD(IphoneUtils.getEquipment());
        ri.setF(IphoneUtils.getFingerprint());
        ri.setI(IphoneUtils.getSystemlevelNum() + "");
        ri.setM(IphoneUtils.getSystemModel());
        ri.setN(IphoneUtils.getNetType(activity));
        ri.setO(IphoneUtils.getSystemVersion());
        ri.setP(IphoneUtils.getDeviceDirective());
            try {
        ri.setR(URLEncoder.encode(IphoneUtils.getOperator(activity),"utf-8"));
        ri.setS(IphoneUtils.getSystemlevel());
        if (IphoneUtils.isCPU64()) {
            Log.e("TAG", "64");
                ri.setT(URLEncoder.encode("64位","utf-8"));
        } else {
            Log.e("TAG", "32");
            ri.setT(URLEncoder.encode("32位","utf-8"));
        }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        ri.setU(IphoneUtils.getRom());
        String toJosn = JsonMapper.getInstance().toJson(ri).toString();
        if (NetWorkUtils.isNetworkConnected(activity)) {
            RetrofitManager retrofitManager = RetrofitManager.getInstance(activity);
            Retrofit retrofitLogin = retrofitManager.getLoginRetrofit();
            ApiUtil apiLogin = retrofitLogin.create(ApiUtil.class);

            apiLogin.getUserStsten(AccountUtil.getInstance().getUid(),
                    AccountUtil.getInstance().getToken(), Base64.encodeToString(toJosn.getBytes(), Base64.NO_WRAP))
                    .enqueue(new MyCallback<ResponseBase<List<SendSystemResponce>>>() {
                        @Override
                        public void onSuccess(ResponseBase<List<SendSystemResponce>> model) {
                            Log.e("TAG", "发送用户信息成功");
                        }

                        @Override
                        public void onFailure(int code, String msg) {
                            if (code == -1) {
                                Log.e("TAG", "发送用户信息失败");
                            }
                        }
                    });
        } else {
            ToastUtils.getInstance(activity).showToastSystem("网络连接失败，再试试吧~");
        }
    }


    private static void getValue(final Activity activity) {
        if (NetWorkUtils.isNetworkConnected(activity) && MyApplication.accountBean!=null) {
            RetrofitManager retrofitManager = RetrofitManager.getInstance(activity);
            Retrofit retrofitLogin = retrofitManager.getLoginRetrofit();
            ApiUtil apiLogin = retrofitLogin.create(ApiUtil.class);

            apiLogin.getRedBnvelope(MyApplication.accountBean.getUid(), MyApplication.accountBean.getToken())
                    .enqueue(new MyCallback<ResponseBase<RedEnvelopeRespone>>() {
                        @Override
                        public void onSuccess(ResponseBase<RedEnvelopeRespone> model) {
                            final RedEnvelopeRespone data = model.getData_info();
                            final RedDataDialog redDataDialog = new RedDataDialog(activity);
                            AccountBean bean = MyApplication.accountBean;
                            bean.setNewAccount(0);
                            bean.setWallet(data.getWallet());
                            AccountUtil.getInstance().saveAccountData(bean);
                            redDataDialog.show();
                            redDataDialog.onClickClose(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    redDataDialog.dismiss();
                                }
                            });

                            redDataDialog.onClickTiXian(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    //跳转提现
                                    redDataDialog.dismiss();
                                    gowebView(activity, data.getWalletUrl());
                                }
                            });
                            redDataDialog.onClickQian(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    //跳转签到
                                    redDataDialog.dismiss();
                                    gowebView(activity, data.getEnergies().getUrl());
                                }
                            });
                            redDataDialog.onClickYao(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    //跳转邀请
                                    redDataDialog.dismiss();
                                    gowebView(activity, data.getShare().getUrl());
                                }
                            });
                            redDataDialog.setValue(data.getMoney()+"元", data.getEnergies().getText(), data.getShare().getText());
                        }

                        @Override
                        public void onFailure(int code, String msg) {
                            ToastUtils.getInstance(activity).showToastSystem(msg);
                        }
                    });
        } else {
            ToastUtils.getInstance(activity).showToastSystem("网络连接失败，再试试吧~");
        }
    }



    private static void gowebView(Activity activity, String url) {
        IntentUtil.getInstance().goShareWebActivity(activity, url);
    }


}
//        Log.e("TAG","照片数量："+ IphoneUtils.getImageSize(MainActivity.this)+",厂商："+IphoneUtils.getDeviceBrand());
//        Log.e("TAG","型号："+ IphoneUtils.getSystemModel()+",系统版本 ："+IphoneUtils.getSystemVersion());
//        Log.e("TAG","运营商："+ IphoneUtils.getOperator(MainActivity.this)+",网络类型 ："+IphoneUtils.getNetType(MainActivity.this));
//        Log.e("TAG","mac地址："+ IphoneUtils.getMac(MainActivity.this)+",系统rom名称 ："+IphoneUtils.getRom());
//        Log.e("TAG","指纹信息："+ IphoneUtils.getFingerprint()+",设备指令集："+IphoneUtils.getDeviceDirective());
//        Log.e("TAG","系统的API级别："+ IphoneUtils.getSystemlevel()+",系统的API级别 num："+IphoneUtils.getSystemlevelNum());
//        Log.e("TAG","获取设备基板名称："+ IphoneUtils.getEquipment());